//
//  KSDatePickerView.m
//  Dingdong
//
//  Created by 程强 on 16/12/1.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "KSDatePickerView.h"

@interface KSDatePickerView()
@property (strong, nonatomic) UIDatePicker *datePicker;
@property (strong, nonatomic) UILabel *titleLbl;
@end

@implementation KSDatePickerView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIButton *backView = [UIButton buttonWithType:UIButtonTypeCustom];
        [backView setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        backView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        [self addSubview:backView];
        [backView addTarget:self action:@selector(removeDatePicker) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, 300)];
        [self addSubview:contentView];
        contentView.backgroundColor = [UIColor whiteColor];
        
        UIToolbar *bar = [[UIToolbar alloc]initWithFrame:CGRectMake(15, 0, frame.size.width-30, 40)];
        bar.barTintColor = [UIColor whiteColor];
        
        UIImage *cancelImage = [LoadImage(@"toolbar_close") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *okImage = [LoadImage(@"toolbar_ok") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithImage:cancelImage style:UIBarButtonItemStylePlain target:self action:@selector(cancelChose)];
        
        UIBarButtonItem *confirmItem = [[UIBarButtonItem alloc]initWithImage:okImage style:UIBarButtonItemStylePlain target:self action:@selector(confirmChose)];
        UIBarButtonItem *fixSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        bar.items = @[cancelItem,fixSpace,confirmItem];
        [contentView addSubview:bar];
        
        self.titleLbl = [[UILabel alloc]initWithFrame:CGRectMake(40, 5, frame.size.width-110, 30)];
        [bar addSubview:_titleLbl];
        _titleLbl.font = [UIFont systemFontOfSize:15.0];
        _titleLbl.textAlignment = NSTextAlignmentCenter;
        
        self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 40, frame.size.width, 260)];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        [contentView addSubview:_datePicker];
        
        self.alpha = 0;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.alpha = 1.0;
            contentView.frame = CGRectMake(0, frame.size.height-300, frame.size.width, 300);
        }];
    }
    return self;
}
- (void)removeDatePicker
{
    [self removeFromSuperview];
}
- (void)cancelChose
{
    [self removeFromSuperview];
}
- (void)confirmChose
{
    NSDate *date = self.datePicker.date;
    if (self.dateBlock) {
        self.dateBlock(date);
    }
    [self cancelChose];
}
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLbl.text = title;
}
- (void)userSelectDateComplete:(SelectDateCallBack)block
{
    self.dateBlock = block;
}
@end
